<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">


        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>项目新增</legend>
        </fieldset>

        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">经费申请人</label>
                <div class="layui-input-block">
                    <input type="text" name="fundsApplicant"  autocomplete="off" placeholder="请输入申请人姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">经费预算</label>
                <div class="layui-input-block">
                    <input type="text" name="fundsBudget" lay-verify="peopleAge" autocomplete="off" placeholder="请输入预算额度/元" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">经费用途说明</label>
                <div class="layui-input-block">
                    <input type="text" name="fundsPurpose" lay-verify="peopleAge" autocomplete="off" placeholder="请输入内容" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">

                <label class="layui-form-label">经费所属社团</label>
                <div class="layui-input-inline">
                    <select id="selectId" name="fundsAssociationId"  lay-verify="required" lay-search="">

                    </select>
                </div>
            </div>
            <div style="height: 20px;">

            </div>



                <div >

                </div>

                <div style="height: 20px;">

                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">申请时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="fundsApplicantTime" id="date" lay-verify="date"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
        </form>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form','jquery','layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,$=layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        //社团下拉框动态加载数据
        $.ajax({
            url:'/select/association/all/data',
            dataType:'json',
            //type:'post',
            success:function(data){

                $.each(data.data,function(index,item){
                    console.log(item);
                    //option  第一个参数是页面显示的值，第二个参数是传递到后台的值
                    $('#selectId').append(new Option(item.communityName,item.communityNumber));//往下拉菜单里添加元素
                    //设置value（这个值就可以是在更新的时候后台传递到前台的值）为2的值为默认选中
                    $('#selectId').val(1);
                })

                form.render(); //更新全部表单内容
                //form.render('select'); //刷新表单select选择框渲染
            }
        });


        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        // //自定义验证规则
        // form.verify({
        //     peopleAge: function (value) {
        //         if (value>100||value<0) {
        //             return '输入正确年龄范围';
        //         }
        //     }
        //     ,title: function (value) {
        //         if (value.length < 5) {
        //             return '标题至少得5个字符啊';
        //         }
        //     }
        //     , pass: [
        //         /^[\S]{6,12}$/
        //         , '密码必须6到12位，且不能出现空格'
        //     ]
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });
        //监听表单提交
        form.render();
        form.on('submit(demo1)', function(data){
            var userInfo = data.field;
            console.log(userInfo);
            var url = "/funds/add";
            $.ajax({
                url:url,
                type:'post',
                dataType: 'json',
                data:userInfo,
                beforeSend:function () {
                    this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                },
                success:function(data){
                   if(data.msg== 'success'){
                       var index = layer.alert("项目信息提交成功！", {
                           title: '系统提示'
                       }, function () {
                           // 关闭弹出层
                           layer.close(index);
                           var iframeIndex = parent.layer.getFrameIndex(window.name);
                           parent.layer.close(iframeIndex);
                           parent.location.reload();//刷新主页表格

                       });
                    }else {
                       layer.msg("提交失败，请检查网络后再试");
                   }
                },
                complete: function () {
                    //layer.close(this.layerIndex);
                },
            });
            return false;//阻止表单跳转
        });

    });
</script>

</body>
</html>